package cn.stylefeng.guns.modular.launchpad.controller;

import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.modular.base.util.Result;
import cn.stylefeng.guns.modular.launchpad.entity.Product;
import cn.stylefeng.guns.modular.launchpad.service.ProductService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/product")
public class ProductController extends BaseController {
    private String PREFIX = "/modular/launchpad/product/";

    @Autowired
    private ProductService productService;


    /**
     * 跳转到产品首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "product.html";
    }

    /**
     * 跳转到添加产品
     */
    @RequestMapping("/product_add")
    public String productAdd() {
        return PREFIX + "product_add.html";
    }

    /**
     * 跳转到修改产品
     */
    @RequestMapping("/product_edit")
    public String productEdit() {
        return PREFIX + "product_edit.html";
    }


    /**
     * 查询列表
     */
    @GetMapping("/list")
    @ResponseBody
    public Result list(Product project) {
        return Result.success(productService.selectByCondition(project));
    }

    // 币种下拉框
    @GetMapping("/coinList")
    @ResponseBody
    public Object coinList(String productId, String type) {
        return productService.coinList(productId, type);
    }

    /**
     * 添加|修改
     */
    @PostMapping("/saveProduct")
    @ResponseBody
    public Result saveProduct(Product project) {
        productService.saveProduct(project);
        return Result.success("添加成功");
    }

    @PostMapping("/changeEnableFlag/{type}")
    @ResponseBody
    public Result changeEnableFlag(@PathVariable("type") String type, @RequestParam Long id) {
        productService.changeEnableFlag(type, id);
        return Result.success();
    }

    @PostMapping("/changeIsHot/{type}")
    @ResponseBody
    public Result changeIsHot(@PathVariable("type") String type, @RequestParam Long id) {
        productService.changeIsHot(type, id);
        return Result.success();
    }

    /**
     * 新币挖矿详情
     */
    @PostMapping(value = "/getData/{id}")
    @ResponseBody
    public Result detail(@PathVariable("id") Long id) {
        Product entity = productService.detail(id);
        return Result.success(entity);
    }


    /**
     * 删除新币挖矿
     */
    @PostMapping("/delete")
    @ResponseBody
    public Result delete(Long id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.productService.deleteProduct(id);
        return Result.success("删除成功");
    }
}
